Java Demystified by James Keogh

Java Demystified by James Keogh

Author:James Keogh
Language: eng
Format: epub, pdf
Publisher: McGraw-Hill Education
Published: 2004-01-20T16:00:00+00:00


Overriding Method Members Using Inheritance

Previously you learned that a method member enables an instance of a class to perform a specific kind of behavior, such as displaying instance variables on the screen. A subclass inherits behavior from a superclass when a subclass can access method members of the superclass. You saw this in the previous example, where the GraduateStudent class called method members of its superclass to display instance variables.

Sometimes the behavior of a method member of a superclass doesn't meet the needs of a subclass. For example, the manner in which a superclass's method member displays an instance variable isn't exactly the way the subclass wants the instance variable displayed.

In this case, a programmer defines another version of the superclass's method member within the subclass and includes statements that enhance the original behavior of the superclass's method member. Programmers referred to this as overriding a method member.

Don't confuse overriding a method member with overloading a method, which you learned about in Chapter 6. Overloading a method requires you to define a method that has the same method name but a different argument list than a method that is already defined. That is, each method has a different signature. Overriding a method member requires you to use the same method name and the same argument list as a method member defined in a subclass's superclass.

You might think that having two method members with the same signature will confuse Java. It won't, and here's why: Java uses method members of the class whose instances call the method. Therefore, if you use an instance of the subclass in your program to call the method member, Java uses the subclass's version of the method member.

Let's take a look at an example to see how this works. The following program is a variation of the program used in the previous example. Here's what is happening in this program: three classes are defined. They are the Person class, the Student class, and the GraduateStudent class. The Person class and the Student class both declare an instance variable, initialize the instance variable, and define a method member called display() that displays its instance variable. Notice that these instance variables are designed with the protected access specifier. This means they can be accessed directly by the subclass.

Neither version of the display() method member is suitable for the GraduateStudent class. Therefore, the GraduateStudent class overrides the display() method member. Statements within the new version of the display() method directly access instance variables of the Person class and the Student class.

An instance of the GraduateStudent class is declared in the main() method, and the instance is used to call the display() method. Java uses the display() method member defined in the GraduateStudent class.

class Demo { public static void main (String args[]) { GraduateStudent gs = new GraduateStudent (); gs.display(); } } class Person { protected String name; Person(){ name = "Bob Smith"; } void display(){ System.out.println("Person Class: " + name); } } class Student extends Person { protected int studentID; Student (){ studentID = 12345; } void display(){ System.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.